import os
import pandas as pd
import numpy as np

pd.set_option('display.unicode.east_asian_width', True)

def get_filenames(path):
    filenames = []
    for file in os.listdir(path):
        if file.endswith('.docx'):  # 如果文件以'.txt'结尾
            filenames.append(file)
    return filenames


# 调用示例
path = r'D:\100-Project\2024-2025-1学期安排\武昌首义学院\24101308_《通信数据分析技术（企业）》\作业和报告\上机报告1'
filenames = get_filenames(path)
# print(filenames)
df = pd.DataFrame(filenames)

se = pd.Series(filenames)
se = se.transform(lambda x: x.split('》')[1])
se = se.transform(lambda x: x.split('.')[0])
se = se.transform(lambda x: x.strip('-'))

se_title = se.transform(lambda x: x.split('-')[0])
se_no = se.transform(lambda x: x.split('-')[1])
se_name = se.transform(lambda x: x.split('-')[2])

df = pd.DataFrame([], index=se_no)
pd.concat([df, se_title, se_name], axis=1)
print(df)

df = pd.DataFrame([se_title, se_name]).T

# df.to_excel(r"D:\tmp.xlsx", index=False)

df.rename(columns={0: 'title', 1: 'name'}, inplace=True)

ret = pd.crosstab(index=df['name'], columns=df['title'], values=df['name'], aggfunc='count')

ret.sort_values(by='name', inplace=True)
print(ret)

"""
title_arr = []
no_arr = []
name_arr = []
for filename in filenames:
    title = filename.split('-')[0].split('》')[1]
    no = filename.split('-')[1]
    name = filename.split('-')[2].split('.')[0]

    no_arr.append(no)
    title_arr.append(title)
    name_arr.append(name)

df = pd.DataFrame({
    'no': no,
    'name': name,
    'title': title
}, index=np.arange(len(title_arr)))

# print(df.loc[df.loc[title] == '上机报告1', :])

# print(df.loc[df['title'] == '上机报告2'])

# print(df.iloc['title'] == '上机报告1')
"""